package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.Examination;
import com.example.demo.util.TestUrl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Copyright © 合生活. All rights reserved.
 *
 * @Title: 习题搜索
 * @Prject: ttc
 * @Package: com.feejson.ttc.web.controller
 * @Description:
 * @author: feejson
 * @date: 2019-06-20 18:23
 * @version: V1.0
 */
@RestController
@Api(description = "合精英学院考试")
@RequestMapping("/hjy")
public class SearchController {
    
    private static final Logger logger = LoggerFactory.getLogger(SearchController.class);


//    @ApiOperation(value = "搜索", notes = "搜索",httpMethod = "GET" )
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "search", value = "部分题目文字", paramType = "query",dataType = "string" ,defaultValue ="婚姻"),
//    })
//    @GetMapping("/searchCase")
    public String getList(String search){
        if (TestUrl.CACHE.size() == 0 ){
            return "没有缓存数据,先拉取数据！";
        }
        String s = "";
        try {
            if (StringUtils.isEmpty(search)){
                return "";
            }
             s = TestUrl.analyzeAllExercises(search);
            if (StringUtils.isEmpty(s)){
                return "这个关键字{" + search + "}没有搜索到!";
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }

//    @ApiOperation(value = "重新拉取习题", notes = "写入文件",httpMethod = "GET" )
//    @GetMapping("/reDrawingExercises")
    public void inputFile(){
        try {
            TestUrl.pullAllTypesOfExercises();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @ApiOperation(value = "完成视频学习", notes = "000000",httpMethod = "GET" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobNumber", value = "工号", paramType = "query",dataType = "string" ,defaultValue ="000000"),
    })
    @Async
    @GetMapping("/quicklyCompleteVideoLearning")
    public String quicklyCompleteVideoLearning(String jobNumber){
        logger.info("jobNumber:"+jobNumber);
        String result =  "";
        try {
            if (StringUtils.isEmpty(jobNumber)){
                return "咩有工号";
            }
            result = TestUrl.changeVodeDone(jobNumber);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    @ApiOperation(value = "完成考试", notes = "000000",httpMethod = "GET" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobNumber", value = "工号", paramType = "query",dataType = "string" ,defaultValue ="000000"),
    })
    @GetMapping("/completeTheExamQuickly")
    @Async
    public String completeTheExamQuickly(String jobNumber){
        String result =  "稍等候刷新";
        try {
            if (StringUtils.isEmpty(jobNumber)){
                return "咩有工号";
            }
//            if (!"563113".equals(jobNumber)){
//                return "Do Not Pass！";
//            }
            Examination.startTheExam(jobNumber);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
